package com.example.bookkeeping_book.service;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.bookkeeping_book.R;
import com.example.bookkeeping_book.utility.MySqlite;
import com.example.bookkeeping_book.utility.Utility;

import java.util.Date;
import java.util.Map;

public class LoginActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        init();
    }

    private void init() {
        Button login;

        CheckBox checkBox;

        TextView tv_register = findViewById(R.id.tv_register);
        login = findViewById(R.id.btn_login);

        EditText account, password;
        account = findViewById(R.id.edt_account);

        password = findViewById(R.id.edt_password);

        checkBox = findViewById(R.id.checkBox);

        Map<String, String> userInfo = Utility.getUserInfo(this);

        if (userInfo.get("isCheck").equals("isCheck")) {
            Intent i = getIntent();
            account.setText(userInfo.get("account"));
            password.setText(userInfo.get("password"));
            checkBox.setChecked(true);
            if (i.getStringExtra("flag") != null && i.getStringExtra("flag").equals("back")) {
                checkBox.setChecked(false);
                password.setText("");
                Utility.saveUserInfo(this, account.getText().toString(), "",false);
            } else {
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        }

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s_account = account.getText().toString().trim();
                String s_password = password.getText().toString().trim();
                if (Utility.selectUserAndPass(new MySqlite(LoginActivity.this), s_account, s_password)) {
                    if (Utility.saveUserInfo(LoginActivity.this, s_account, s_password, checkBox.isChecked())) {
                        Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(LoginActivity.this, "存储失败", Toast.LENGTH_SHORT).show();

                    }
                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                } else {
                    Toast.makeText(LoginActivity.this, "账号或密码错误", Toast.LENGTH_SHORT).show();
                }


            }
        });

        tv_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });

    }
}